var TreasureControllerUIListener = function(scope, treasureService){
	var $scope = scope;
	
	 $scope.onSearchTreasureTemplates = function(treasureTemplateName){
		 var modalDialog = $("#newTreasureTemplateDialog");
		 var modalErrorLabel = $("#newTreasureTemplateErrorLbl");
		 var onSuccess = function(treasureTemplates){
			 $scope.treasureTemplates = treasureTemplates;
			 if($scope.treasureTemplates.length > 0){
				 $scope.selectedTreasureTemplate = $scope.treasureTemplates[0]; // This needs to be resolved somehow
			 }
		 };
		 var onFailure = function(message){
			 modalErrorLabel.text(message);
			 modalErrorLabel.show();
		 };
		 var onError = function(status, message){
			 modalErrorLabel.text("Request Error: " + message );
			 modalErrorLabel.show();
		 }; 
		 
		 $scope.treasureTemplates = treasureService.searchTreasureTemplates(treasureTemplateName, onSuccess, onFailure, onError);
	 };
	 
     $scope.onFileSelectedChange = function(fileElement){
    	 if(fileElement.files.length >0){
    		 $scope.selectedTreasureTemplate.content.name = fileElement.files[0].name; 
    		 $scope.selectedTreasureTemplate.content.size = fileElement.files[0].size; 
    		 $scope.selectedTreasureTemplate.content.type = fileElement.files[0].type; 
    		 $scope.$apply();
    		 $scope.handleFilePreview(fileElement.files[0]);
    	 }
     };
      
     $scope.onNewTreasureTemplateBtnClick = function(){
    	 // I meant to add this on document ready but there is some wierdness happening when binding there.
		 $('#newTreasureTemplateDialog').on('shown.bs.modal', function () {
				$('#newTreasureTemplateNameTxt').focus();
		 });
    	 $scope.clearNewTreasureTemplateDialog();
     };
     
     $scope.onSaveTreasureTemplateClick = function(){
    	 //$scope.kyles();
		 if( $scope.selectedTreasureTemplate ){	
			 $scope.updateTreasureTemplate();
		 }
		 else{
		     $scope.showMessageDialog("No Template Selected", "Please select a template to save", false); 
		 }
     };
     
	  
	 $scope.onCreateNewTreasureTemplate = function(){
		 var modalDialog = $("#newTreasureTemplateDialog");
		 var modalErrorLabel = $("#newTreasureTemplateErrorLbl");
		 var onSuccess = function(treasureTemplate){
			 $scope.treasureTemplates.push(treasureTemplate);
			 $scope.selectedTreasureTemplate = $scope.treasureTemplates[$scope.treasureTemplates.length-1]; 
			 modalDialog.modal('hide');
		 };
		 var onFailure = function(message){
			 modalErrorLabel.text(message);
			 modalErrorLabel.show();
		 };
		 var onError = function(status, message){
			 modalErrorLabel.text("Request Error: " + message );
			 modalErrorLabel.show();
		 }; 
		 treasureService.createNewTreasureTemplate($scope.newTreasureTemplateName, onSuccess, onFailure, onError);
	 };
	 
	 
	 $scope.onNewTreasureTemplateNameTxtKeyUp = function(event){
		 // 13 enter key
		    if(event.keyCode == 13){
		    	$scope.onCreateNewTreasureTemplate();
		    }
	 };
};


angular.module('mainApp').value("treasureControllerUIListener", TreasureControllerUIListener);